//
//  BankSettingViewController.m
//  MoneyBook
//
//  Created by Justin Kim on 11. 6. 27..
//  Copyright 2011 Daum. All rights reserved.
//

#import "BankSettingViewController.h"


@implementation BankSettingViewController
/////////////////////////////////////////////////////////////////////////////////////////////////
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
    }   
    return self;
}

/////////////////////////////////////////////////////////////////////////////////////////////////
- (void)dealloc{
    [super dealloc];
}

/////////////////////////////////////////////////////////////////////////////////////////////////
- (void)didReceiveMemoryWarning{
    [super didReceiveMemoryWarning];
}

#pragma mark - View lifecycle
/////////////////////////////////////////////////////////////////////////////////////////////////
- (void)viewDidLoad{
    self.title = @"은행 관리";
    [self.tableView setAllowsSelectionDuringEditing:YES];
    
    [super viewDidLoad];
}

/////////////////////////////////////////////////////////////////////////////////////////////////
- (void)viewDidUnload{
    [super viewDidUnload];
}

/////////////////////////////////////////////////////////////////////////////////////////////////
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.dataSourceArray = [[SQLHandler sharedInstance] findAllBank];    
    [self.tableView reloadData];
}

/////////////////////////////////////////////////////////////////////////////////////////////////
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
}

/////////////////////////////////////////////////////////////////////////////////////////////////
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{
	[super shouldAutorotateToInterfaceOrientation:interfaceOrientation];
    return YES;
}

/////////////////////////////////////////////////////////////////////////////////////////////////
- (void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
    [super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
    
}
/////////////////////////////////////////////////////////////////////////////////////////////////
- (void)showEditMode{
    _isEditMode = YES;
    [self.tableView setEditing:YES animated:YES];
    UIBarButtonItem *rightBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
                                                                              target:self
                                                                              action:@selector(showDoneMode)];
    [self.navigationItem setRightBarButtonItem:rightBtn];        
    [rightBtn release];
}

#pragma mark -
#pragma mark UIBarButtonItemDelegate
/////////////////////////////////////////////////////////////////////////////////////////////////
- (void)addAction{
    BankFormViewController* viewController = [[[BankFormViewController alloc] initWithNibName:@"BankFormView" bundle:nil] autorelease];
    [[self navigationController] pushViewController:viewController animated:YES];        
    [viewController createMode];
}
#pragma mark -
#pragma mark Utils
/////////////////////////////////////////////////////////////////////////////////////////////////
- (NSString*)bankType:(NSNumber*)bankType{
    switch ([bankType intValue]) {
        case 0:
            return @" [예금]";
            break;
        case 1:
            return @" [적금]";
            break;
        case 2:
            return @" [대출]";
            break;
        case 3:
            return @" [주식]";
            break;
        default:
            break;
    }
    return @"";
}
#pragma mark -
#pragma mark UITableViewDelegate
/////////////////////////////////////////////////////////////////////////////////////////////////
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    BankFormViewController* viewController = [[[BankFormViewController alloc] initWithNibName:@"BankFormView" bundle:nil] autorelease];
    [[self navigationController] pushViewController:viewController animated:YES];        
    [viewController loadBank:[[_dataSourceArray objectAtIndex:indexPath.row] objectForKey:@"bank_id"]];        
}
/////////////////////////////////////////////////////////////////////////////////////////////////
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    SQLHandler *sql = [SQLHandler sharedInstance];
    [sql deleteBankById:[[_dataSourceArray objectAtIndex:indexPath.row] objectForKey:@"bank_id"]];
    self.dataSourceArray = [sql findAllBank];
    [self.tableView reloadData];
}
/////////////////////////////////////////////////////////////////////////////////////////////////
-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath{
    [self moveRowAtIndexPath:fromIndexPath
                 toIndexPath:toIndexPath 
                   tableName:@"bank"];
}
/////////////////////////////////////////////////////////////////////////////////////////////////
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier];
	if (cell == nil){
		cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:kCellIdentifier] autorelease];
		cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.textLabel.textAlignment = UITextAlignmentCenter;
        cell.textLabel.textColor = [UIColor grayColor];
        cell.textLabel.highlightedTextColor = [UIColor blackColor];
        cell.textLabel.font = [UIFont systemFontOfSize:12.0];
        cell.showsReorderControl = YES;
	}
    cell.textLabel.text = [[[_dataSourceArray objectAtIndex:indexPath.row] objectForKey:@"title"] stringByAppendingString:[self bankType:[[_dataSourceArray objectAtIndex:indexPath.row] objectForKey:@"type"]]];
	
	return cell;
}

@end
